//
//  YXNewfeatureViewController.m
//  Newfeature
//
//  Created by 周丽丽 on 16/8/22.
//  Copyright © 2016年 周丽丽. All rights reserved.
//

#import "YXNewfeatureViewController.h"
#import "UIView+YXViewExtension.m"
#import "YXTabBarController.h"
#import "YXNewfeaturePageView.h"
#import "YXNewcharcateristicInterfaceModel.h"
@interface YXNewfeatureViewController ()<UIScrollViewDelegate,YXNewfeaturePageViewDelegate>
@property (nonatomic, weak) UIPageControl *pageControl;
@property (nonatomic, strong) NSArray *textDataArr;
@end
@implementation YXNewfeatureViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [UIApplication sharedApplication].statusBarHidden = YES;
    [self setupTextDataArr];
    [self setupScrollView];
    [self setupPageControl];
}
- (void)setupTextDataArr{
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NewCharacteristicInterface.plist"  ofType:nil]];
    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:array.count];
    for (NSDictionary *dict in array) {
        [arrayM addObject:[[YXNewcharcateristicInterfaceModel alloc]initWithDictionary:dict error:nil]];
    }
    self.textDataArr = arrayM.copy;
}
- (void)setupScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.bounces = NO;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    CGFloat PageviewW = 375 * AutoSizeScaleX;
    CGFloat PageviewH = 667 * AutoSizeScaleY;
    for (int i = 0; i<YXNewfeatureImageCount; i++) {
        YXNewfeaturePageView *pageview = [[YXNewfeaturePageView alloc]init];
        [scrollView addSubview:pageview];
        pageview.y = 0;
        pageview.width = PageviewW;
        pageview.height = PageviewH;
        pageview.x = i * pageview.width;
        pageview.delegate = self;
        pageview.textDataModel = self.textDataArr[i];
        [pageview setupPageViewWithIndex:i];
}
    scrollView.contentSize = CGSizeMake(YXNewfeatureImageCount * PageviewW, 0);
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor whiteColor];
}
- (void)setupPageControl
{
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = YXNewfeatureImageCount;
    [self.view addSubview:pageControl];
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-30 * AutoSizeScaleX);
        make.centerX.equalTo(self.view);
    }];
    self.pageControl = pageControl;
    [self changePageControlImage:self.pageControl];
}
- (void)changePageControlImage:(UIPageControl *)pageControl
{
    pageControl.currentPageIndicatorTintColor = [UIColor colorWithHexString:@"989898"];
    pageControl.pageIndicatorTintColor = [UIColor colorWithHexString:@"e2e2e2"];
}
#pragma mark - YXNewfeaturePageViewDelegate
- (void)startButtonClick:(UIButton *)startButton
{
    [UIApplication sharedApplication].statusBarHidden = NO;
        YXTabBarController *tabVC = [[YXTabBarController alloc]init];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        window.rootViewController = tabVC;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat page = scrollView.contentOffset.x / scrollView.width;
    int pageCount = (int)(page + 0.5);
    self.pageControl.currentPage = pageCount;
    [self changePageControlImage:self.pageControl];
}
@end



